home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / kerberos / pc / krb_libd.lha / Lib / Des / TESTIT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-03  |  5.7 KB  |  243 lines

  1. /*
  2.  * $Source: /mit/kerberos/src/lib/des/RCS/testit.c,v $
  3.  * $Author: jtkohl $
  4.  *
  5.  * Copyright 1988 by the Massachusetts Institute of Technology.
  6.  *
  7.  * For copying and distribution information, please see the file
  8.  * <mit-copyright.h>.
  9.  *
  10.  * exit returns     0 ==> success
  11.  *        -1 ==> error
  12.  */
  13.  
  14. #ifndef    lint
  15. static char rcsid_testit_c[] =
  16. "$Header: testit.c,v 4.7 88/11/15 11:39:23 jtkohl Exp $";
  17. #endif    lint
  18.  
  19. #include <mit_copy.h>
  20. #include <stdio.h>
  21. #include <errno.h>
  22. #include <des.h>
  23.  
  24. #define MIN_ARGC    0    /* min # args, not incl flags */
  25. #define MAX_ARGC    2    /* max # args, not incl flags */
  26.  
  27. /* MIN_ARGC == MAX_ARGC ==> required # args */
  28.  
  29. extern char *errmsg();
  30. extern int des_string_to_key();
  31. extern int des_key_sched();
  32. extern int des_ecb_encrypt();
  33. extern int des_cbc_encrypt();
  34. extern int des_pcbc_encrypt();
  35. char *progname;
  36. int sflag;
  37. int vflag;
  38. int tflag;
  39. int nflag = 1000;
  40. int cflag;
  41. int des_debug ;
  42. des_key_schedule KS;
  43. unsigned char cipher_text[64];
  44. unsigned char clear_text[64] = "Now is the time for all " ;
  45. unsigned char clear_text2[64] = "7654321 Now is the time for ";
  46. unsigned char clear_text3[64] = {2,0,0,0, 1,0,0,0};
  47. unsigned char *input;
  48.  
  49. /* 0x0123456789abcdef */
  50. des_cblock default_key = { 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef };
  51. des_cblock s_key;
  52. des_cblock default_ivec = { 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef };
  53. unsigned char *ivec;
  54. des_cblock zero_key = {1};
  55. int i,j;
  56.  
  57. main(argc,argv)
  58.     int argc;
  59.     char *argv[];
  60. {
  61.     /*    Local Declarations */
  62.  
  63.     long in_length;
  64.  
  65.     progname=argv[0];            /* salt away invoking program */
  66.  
  67.     /* Assume a long is four bytes */
  68.     if (sizeof(long) != 4) {
  69.     fprintf(stdout,"\nERROR,  size of long is %d",sizeof(long));
  70.     exit(-1);
  71.     }
  72.  
  73.     while (--argc > 0 && (*++argv)[0] == '-')
  74.     for (i=1; argv[0][i] != '\0'; i++) {
  75.         switch (argv[0][i]) {
  76.  
  77.         /* debug flag */
  78.         case 'd':
  79.         des_debug=1;
  80.         continue;
  81.  
  82.         /* verbose flag */
  83.         case 'v':
  84.         vflag = 1;
  85.         continue;
  86.  
  87.         /* cbc flag */
  88.         case 'c':
  89.         cflag=1;
  90.         continue;
  91.  
  92.         /* string to key only flag */
  93.         case 's':
  94.         sflag = 1;
  95.         continue;
  96.  
  97.         /* test flag - use known key and cleartext */
  98.         case 't':
  99.         tflag=1;
  100.         continue;
  101.  
  102.         /* iteration count */
  103.         case 'n':
  104.         sscanf(&argv[0][i+1],"%d",&nflag);
  105.         argv[0][i+1] = '\0'; /* force it to stop */
  106.         break;
  107.  
  108.         default:
  109.         printf("%s: illegal flag \"%c\" ",
  110.                progname,argv[0][i]);
  111.         exit(1);
  112.         }
  113.     };
  114.  
  115.     if (argc < MIN_ARGC || argc >MAX_ARGC) {
  116.     printf("Usage: xxx [-xxx]  xxx xxx\n");
  117.     exit(1);
  118.     }
  119.  
  120.     /* argv[0] now points to first non-option arg, if any */
  121.  
  122.     if (tflag) {
  123.     /* use known input and key */
  124.     des_key_sched(default_key,KS);
  125.     input = clear_text;
  126.     ivec = (unsigned char *) default_ivec;
  127.     }
  128.     else {
  129.     /*des_string_to_key(argv[0],s_key); */
  130.     des_string_to_key("test",s_key);
  131.     if (vflag) {
  132.         input = (unsigned char *) s_key;
  133.         fprintf(stdout,"\nstring = %s, key = ",argv[0]);
  134.         for (i = 0; i<=7 ; i++) fprintf(stdout,"%02x ",*input++);
  135.     }
  136.     des_string_to_key("test",s_key);
  137.     if (vflag) {
  138.         input = (unsigned char *) s_key;
  139.         fprintf(stdout,"\nstring = %s, key = ",argv[0]);
  140.         for (i = 0; i<=7 ; i++) fprintf(stdout,"%02x ",*input++);
  141.     }
  142.     des_key_sched(s_key,KS);
  143.     input = (unsigned char *) argv[1];
  144.     ivec = (unsigned char *)  argv[2];
  145.     }
  146.  
  147.  
  148.     if (cflag) {
  149.     fprintf(stdout,"\nclear %s\n",input);
  150.     in_length = strlen(input);
  151.     des_cbc_encrypt(input,cipher_text,(long) in_length,KS,ivec,1);
  152.     fprintf(stdout,
  153.         "\n\nencrypted ciphertext = (low to high bytes)");
  154.     for (i = 0; i <= 7; i++) {
  155.         fprintf(stdout,"\n");
  156.         for (j = 0; j <= 7; j++)
  157.         fprintf(stdout,"%02x ",cipher_text[i*8+j]);
  158.     }
  159.     des_cbc_encrypt(cipher_text, clear_text,
  160.             (long) in_length, KS, ivec, 0);
  161.     fprintf(stdout,"\n\ndecrypted clear_text = %s",clear_text);
  162.  
  163.     fprintf(stdout,"\nclear %s\n",input);
  164.     input = clear_text2;
  165.     des_cbc_cksum(input,cipher_text,(long) strlen(input),KS,ivec,1);
  166.     fprintf(stdout,
  167.         "\n\nencrypted cksum = (low to high bytes)\n");
  168.     for (j = 0; j <= 7; j++)
  169.         fprintf(stdout,"%02x ",cipher_text[j]);
  170.  
  171.     /* test out random number generator */
  172.     for (i = 0; i <= 7; i++) {
  173.         des_random_key(cipher_text);
  174.         des_key_sched(cipher_text,KS);
  175.         fprintf(stdout,
  176.             "\n\nrandom key = (low to high bytes)\n");
  177.         for (j = 0; j<=7; j++)
  178.         fprintf(stdout,"%02x ",cipher_text[j]);
  179.     }
  180.     }
  181.     else {
  182.     if (vflag)
  183.         fprintf(stdout,"\nclear %s\n",input);
  184.     do_encrypt(input,cipher_text);
  185.     do_decrypt(clear_text,cipher_text);
  186.     }
  187. }
  188.  
  189. flip(array)
  190.     char *array;
  191. {
  192.     register old,new,i,j;
  193.     /* flips the bit order within each byte from 0 lsb to 0 msb */
  194.     for (i = 0; i <= 7; i++) {
  195.     old = *array;
  196.     new = 0;
  197.     for (j = 0; j <= 7; j++) {
  198.         if (old & 01) new = new | 01;
  199.         if (j < 7) {
  200.         old = old >> 1;
  201.         new = new << 1;
  202.         }
  203.     }
  204.     *array = new;
  205.     array++;
  206.     }
  207. }
  208.  
  209. do_encrypt(in,out)
  210.     unsigned char    *in;
  211.     unsigned char    *out;
  212. {
  213.     for (i = 1; i <= nflag; i++) {
  214.     des_ecb_encrypt(in,out,KS,1);
  215.     if (vflag) {
  216.         fprintf(stdout,"\nclear %s\n",in);
  217.         for (j = 0; j <= 7; j++)
  218.         fprintf(stdout,"%02 X ",in[j] & 0xff);
  219.         fprintf(stdout,"\tcipher ");
  220.         for (j = 0; j<=7; j++)
  221.         fprintf(stdout,"%02X ",out[j] & 0xff);
  222.     }
  223.     }
  224. }
  225.  
  226. do_decrypt(in,out)
  227.     unsigned char    *out;
  228.     unsigned char    *in;
  229.     /* try to invert it */
  230. {
  231.     for (i = 1; i <= nflag; i++) {
  232.     des_ecb_encrypt(out,in,KS,0);
  233.     if (vflag) {
  234.         fprintf(stdout,"\nclear %s\n",in);
  235.         for (j = 0; j <= 7; j++)
  236.         fprintf(stdout,"%02X ",in[j] & 0xff);
  237.         fprintf(stdout,"\tcipher ");
  238.         for (j = 0; j<=7; j++)
  239.         fprintf(stdout,"%02X ",out[j] & 0xff);
  240.     }
  241.     }
  242. }
  243.